package cn.edu.ujn.finaltest.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mysql.jdbc.StringUtils;

import cn.edu.ujn.finaltest.dao.Customer;
import cn.edu.ujn.finaltest.dao.CustomerMapper;
import cn.edu.ujn.finaltest.util.Page;

@Service("customerService")
@Transactional
public class CustomerServiceImpl implements ICustomerService {
	
	@Autowired
	private CustomerMapper customerDao;

	@Override
	public int deleteByPrimaryKey(Integer custId) {
		// TODO Auto-generated method stub
		return this.customerDao.deleteByPrimaryKey(custId);
	}

	@Override
	public int insert(Customer row) {
		// TODO Auto-generated method stub
		return this.customerDao.insert(row);
	}

	@Override 	
	public int insertSelective(Customer row) {
		// TODO Auto-generated method stub
		return this.customerDao.insertSelective(row);
	}

	@Override
	public Customer selectByPrimaryKey(Integer custId) {
		// TODO Auto-generated method stub
		return this.customerDao.selectByPrimaryKey(custId);
	}

	@Override
	public int updateByPrimaryKeySelective(Customer row) {
		// TODO Auto-generated method stub
		return this.customerDao.updateByPrimaryKeySelective(row);
	}

	@Override
	public int updateByPrimaryKey(Customer row) {
		// TODO Auto-generated method stub
		return this.customerDao.updateByPrimaryKey(row);
	}

	@Override
	public Integer selectCustomerListCount(Customer customer) {
		// TODO Auto-generated method stub
		return this.customerDao.selectCustomerListCount(customer);
	}

	@Override
	public Page<Customer> findCustomerList(Integer page, Integer rows, String custName, String custSource,
			String custIndustry, String custLevel) {
		// TODO Auto-generated method stub
        Customer customer = new Customer();
        if(!StringUtils.isNullOrEmpty(custName)) {
        	customer.setCust_name(custName);
        }
		if(!StringUtils.isNullOrEmpty(custSource)){
			customer.setCust_source(custSource);
		}
		if(!StringUtils.isNullOrEmpty(custIndustry)){
			customer.setCust_industry(custIndustry);
		}
		if(!StringUtils.isNullOrEmpty(custLevel)){
			customer.setCust_level(custLevel);
		}
		customer.setStart((page-1) * rows) ;
		customer.setRows(rows);
		List<Customer> customers = this.customerDao.selectCustomerList(customer);
		// 查询客户列表总记录数
		Integer count = customerDao.selectCustomerListCount(customer);
		// 创建Page返回对象
		Page<Customer> result = new Page<Customer>();
		result.setPage(page);
		result.setRows(customers);
		result.setSize(rows);
		result.setTotal(count);
		return result;
	}
	
}
